/*
 * @Author: Kilims Ye
 * @Date: 2021-03-03 14:53:54
 * @LastEditTime: 2021-03-31 09:36:39
 * @LastEditors: Kilims Ye
 * @Description:
 * @FilePath: /react-mobile-seeds/src/services/index.js
 */
import apiConfig from "./apiConfig";
import { urlParams } from "@utils/UtilsFunc";
import _randkey from "@utils/randkey";
import uuid from "uuid-random";
import Validater from "@utils/Validater";
import { customizedPush } from "@routes/history";
import _R from "./fetchConfig/fetch_util";
import { Toast } from "antd-mobile";

const env = process.env.REACT_APP_ENV;
const routeName = process.env.REACT_APP_ROUTE_NAME;

let currentHost = `${apiConfig.HOST[env]}`;
currentHost = "DEV";

currentHost = process.env.NODE_ENV === "production" ? `${apiConfig.HOST[env]}` : currentHost;

const commonPromiseHandler = (uri, params, isFormData) => {
  return new Promise((resolve, reject) => {
    _R[`${isFormData ? "customizedFetch" : "fetch"}`](uri, params)
      .then((res) => {
        const {
          errcode, errmsg
        } = res
        if(errcode){
          Toast.fail(errmsg)
          reject(res)
        } else {
          resolve(res)
        }
      })
      .catch((e) => {
        reject(e);
        console.log("失败 e", e);
      });
  });
};

const api = {
  createWxUser(openId, nickName, gender, city, province, country, headImgUrl, appId) {
    return commonPromiseHandler(`${currentHost}/${apiConfig.URI.CREATE_WX_USER}`, {
      openId: openId,
      nickName: nickName,
      gender: gender,
      city: city,
      province: province,
      country: country,
      headImgUrl: headImgUrl,
      appId: appId,
    });
  }
};

export default api;
